package mumu.android.lib.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.GridView;

public class GridViewPager<T> extends ViewPager {

	private Context mContext;
	private List<View> mListViews = new ArrayList<View>();// viewpager适配器数据
	private int pageSize = 0;// 总共多少页
	private int onePageSize = 0;// 一页数量
	private int column, rows;// 列行数
	private int verticalSpacing = 20;// 垂直间距
	private int horizontalSpacing = 20;// 水平间距
	private MPagerAdapter mPagerAdapter;
	private List<T> mListDatas = new ArrayList<T>();// 每个item的数据
	private int gvItemWidth, gvItemHeight;// 每griditem的宽高
	private int mWidth, mHeight;// 控件宽高
	private Boolean hasMeasure = false;// 是否已经测量过宽高
	private IWatchInterface<T> mIWatchInterface;

	public GridViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;

	}

	public GridViewPager(Context context) {
		super(context);
		this.mContext = context;
	}

	/**
	 * 初始化view
	 * 
	 * @param mWatchInterface
	 * @param column
	 * @param rows
	 * @param list
	 */
	public void createView(final IWatchInterface<T> mWatchInterface, final int column, final int rows, final List<T> list) {
		this.column = column;
		this.rows = rows;
		this.onePageSize = column * rows;
		this.mListDatas = list;
		this.pageSize = list.size() / onePageSize + (list.size() % onePageSize >= 1 ? 1 : 0);
		
		this.mIWatchInterface = mWatchInterface;
		ViewTreeObserver vto = getViewTreeObserver();
		vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
			public boolean onPreDraw() {
				if (!hasMeasure) {
					mHeight = GridViewPager.this.getMeasuredHeight();
					mWidth = GridViewPager.this.getMeasuredWidth();
					GridViewPager.this.init(column, rows, list);
					hasMeasure = true;
				}
				return true;
			}
		});
	}

	/**
	 * 初始化内部view
	 * 
	 * @param column
	 * @param rows
	 * @param list
	 */
	void init(int column, int rows, List<T> list) {
		this.column = column;
		this.rows = rows;
		this.onePageSize = column * rows;
		this.mListDatas = list;
		this.pageSize = list.size() / onePageSize + (list.size() % onePageSize >= 1 ? 1 : 0);
		this.gvItemHeight = (mHeight - (rows - 1) * verticalSpacing) / rows;
		this.gvItemWidth = (mWidth - (column - 1) * horizontalSpacing) / column;

		mListViews.clear();
		for (int i = 0; i < pageSize; i++) {
			GridView gView = new GridView(mContext);
			gView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			gView.setNumColumns(column);
			gView.setSelector(new ColorDrawable(Color.TRANSPARENT));
			gView.setVerticalSpacing(verticalSpacing);
			gView.setHorizontalSpacing(horizontalSpacing);

			List<T> mListdata = new ArrayList<T>();
			for (int j = i * onePageSize; j < (i + 1) * onePageSize; j++) {
				if (j >= mListDatas.size())
					break;
				mListdata.add(mListDatas.get(j));
			}
			GvAdapter gvAdapter = new GvAdapter(mListdata);
			gView.setAdapter(gvAdapter);
			mListViews.add(gView);
		}
		mPagerAdapter = new MPagerAdapter();
		setAdapter(mPagerAdapter);
	}

	public interface IWatchInterface<T> {
		View gvGetView(T t, int position, int itemWidth, int itemHeight);
	}

	// public void setIWatchInterface(IWatchInterface<T> mIWatchInterface) {
	// this.mIWatchInterface = mIWatchInterface;
	// }

	/**
	 * 分页内gridview适配器
	 * 
	 * @author Administrator
	 * 
	 */
	public class GvAdapter extends BaseAdapter {

		List<T> mGvListDatas = new ArrayList<T>();

		public GvAdapter(List<T> mGvListDatas) {
			this.mGvListDatas = mGvListDatas;
		}

		@Override
		public int getCount() {
			return this.mGvListDatas.size();
		}

		@Override
		public T getItem(int arg0) {
			return this.mGvListDatas.get(arg0);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			T t = getItem(position);
			if (mIWatchInterface != null)
				convertView = mIWatchInterface.gvGetView(t, position, gvItemWidth, gvItemHeight);
			return convertView;
		}
	}

	/**
	 * 数据更新
	 * 
	 * @param column
	 * @param rows
	 * @param list
	 */
	public void notifyDataSetChanged(int column, int rows, List<T> list) {
		this.removeAllViews();
		init(column, rows, list);
		this.setAdapter(mPagerAdapter);
		mPagerAdapter.notifyDataSetChanged();
	}

	/**
	 * 频道分页适配器
	 * 
	 * @author Administrator
	 * 
	 */
	public class MPagerAdapter extends PagerAdapter {
		@Override
		public int getItemPosition(Object object) {
			// return super.getItemPosition(object);
			return POSITION_NONE;// 修复viewpager内部视图不及时更新bug
		}

		public MPagerAdapter() {

		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			if (position >= mListViews.size())
				return;
			container.removeView(mListViews.get(position));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(mListViews.get(position), 0);
			return mListViews.get(position);
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

	}
}
